﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OracleClient;

namespace Projekt_GUI.Forms
{
    public partial class ReisenUebersicht : Form
    {
        OracleHelper oracle { get { return OracleHelper.Instance; } }
        public ReisenUebersicht()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;
            Point openUpHere = new Point(857, 0);
            this.Location = openUpHere;
        }

        private void ReisenUebersicht_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {           
            oracle.Open();

            int counter = 0;
            listView1.Items.Clear();

            OracleDataReader r = oracle.Select("Beschreibung, Location, Preis, Plaetze, Nummer", "Reisen");
            while (r.Read())
            {
                listView1.Items.Add(r.GetValue(0).ToString());
                listView1.Items[counter].SubItems.Add(r.GetValue(1).ToString());
                listView1.Items[counter].SubItems.Add(r.GetValue(2).ToString());
                listView1.Items[counter].SubItems.Add(r.GetValue(3).ToString());
                listView1.Items[counter].SubItems.Add(r.GetValue(4).ToString());
                counter++;
            }

            oracle.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Reisen r = new Reisen();
            r.MdiParent = this.MdiParent;
            r.Show();

            Refresh();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                oracle.Open();                    
                oracle.Delete("Reisen", "Nummer = " + listView1.SelectedItems[0].SubItems[4].Text);
                Refresh();
            }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (((ToolStripButton)sender).Text == "Filter")
            {
                ReisenFilter rf = new ReisenFilter();
                DialogResult res = rf.ShowDialog();
                if (res == System.Windows.Forms.DialogResult.OK)
                {
                    using (OracleConnection con = new OracleConnection("Data Source=localhost; User Id=PROJEKT; Password=PROJEKT"))
                    {
                        int FilterCounter = 0;

                        con.Open();
                        OracleCommand com = con.CreateCommand();
                        com.CommandText = "SELECT Beschreibung, Location, Preis, Plaetze, Nummer FROM Reisen";

                        if (rf.BoxID.Text != "")
                        {
                            if (FilterCounter == 0)
                            {
                                com.CommandText += " WHERE ID = " + rf.BoxID.Text;
                            }
                            else
                            {
                                com.CommandText += " AND ID = " + rf.BoxID.Text;
                            }
                            FilterCounter++;
                        }
                        if (rf.BoxName.Text != "")
                        {
                            if (FilterCounter == 0)
                            {
                                com.CommandText += " WHERE Beschreibung LIKE '%" + rf.BoxName.Text + "%'";
                            }
                            else
                            {
                                com.CommandText += " AND Beschreibung LIKE '%" + rf.BoxName.Text + "%'";
                            }
                            FilterCounter++;
                        }
                        if (rf.BoxPreis.Text != "")
                        {
                            if (FilterCounter == 0)
                            {
                                com.CommandText += " WHERE Preis " + rf.ComboPreis.SelectedItem.ToString() + " " + rf.BoxPreis.Text;
                            }
                            else
                            {
                                com.CommandText += " AND Preis " + rf.ComboPreis.SelectedItem.ToString() + " " + rf.BoxPreis.Text;
                            }
                            FilterCounter++;
                        }

                        OracleDataReader r = com.ExecuteReader();

                        int counter = 0;
                        listView1.Items.Clear();
                        while (r.Read())
                        {
                            listView1.Items.Add(r.GetValue(0).ToString());
                            listView1.Items[counter].SubItems.Add(r.GetValue(1).ToString());
                            listView1.Items[counter].SubItems.Add(r.GetValue(2).ToString());
                            listView1.Items[counter].SubItems.Add(r.GetValue(3).ToString());
                            listView1.Items[counter].SubItems.Add(r.GetValue(4).ToString());
                            counter++;
                        }
                    }
                }
                ((ToolStripButton)sender).Text = "Filter löschen";
                ((ToolStripButton)sender).Image = imageList1.Images[1];
            }
            else
            {
                Refresh();
                ((ToolStripButton)sender).Text = "Filter";
                ((ToolStripButton)sender).Image = imageList1.Images[0];
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ReisenAnsicht ra = new ReisenAnsicht(listView1.SelectedItems[0].SubItems[4].Text);
                ra.MdiParent = this.MdiParent;
                ra.Show();
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                Form1.Cart.Add((Form1.Cart.Count+1).ToString(), listView1.SelectedItems[0].SubItems[4].Text);
                MessageBox.Show("Die Reise wurde dem Warenkorb hinzugefügt.", "Warenkorb", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            
            if (listView1.SelectedItems.Count > 0)
            {
                Reisen r = new Reisen(listView1.SelectedItems[0].SubItems[4].Text);
                r.ShowDialog();
                Refresh();
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}
